import axios from 'axios'

const http = axios.create({
    // baseURL:'http://192.168.43.160:5000',
    baseURL:'http://localhost:5000',
    timeout:'3000',
    // 请求头配置
    header:{
        'Content-Type': 'application/json',
    }
})


// 添加请求拦截器
http.interceptors.request.use(function (config) {
  const token = sessionStorage.getItem('token');
  config.headers['Authorization'] = token ? `Bearer ${token}` : ''; // 设置 Authorization 请求头
  return config;
}, function (error) {

  return Promise.reject(error);
});

// 添加响应拦截器
http.interceptors.response.use(function (response) {
  
    if(response.status ===204){
        ElNotification({
            title: '消息提示',
            message: '无内容',
            type: 'warning',
          })
    }else if(response.status === 301){
        ElNotification({
            title: '消息提示',
            message: '永久重定向',
            type: 'warning',
          })
    }else if(response.status === 404){
        ElNotification({
            title: '消息提示',
            message: '找不到网页',
            type: 'warning',
          })
    }else if(response.status === 500){
        ElNotification({
            title: '消息提示',
            message: '服务器错误',
            type: 'warning',
          })
    }

    return response;
  }, function (error) {
    return Promise.reject(error);
  });

export default http;
